import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class DaoCase {
  late Database database;

  // Future<void> sqlInit() async {
  //   var databasesPath = await getDatabasesPath();
  //   String path = join(databasesPath, "word.db");
  //   database = await openDatabase(path, version: 1,
  //       onCreate: (Database db, int version) async {
  //     // 当打开数据库的时候创建一张表
  //     // await db.execute(
  //     //     "CREATE TABLE t_Case (id INTEGER PRIMARY KEY, caseName TEXT, "
  //     //     "caseTypeId INTEGER, caseNum INTEGER, caseCondition TEXT, "
  //     //     "caseContent TEXT, caseflog NUMERIC)");
  //   });
  // }
  //表结构，id，word，meaning，favorite

  Future<void> creaatTable() async {
    await database
        .execute("CREATE TABLE t_word (_id INTEGER PRIMARY KEY, _word TEXT, _meaning TEXT, _voice TEXT, _favorite INTEGER)");
  }

  isTableExits() async {
    //内建表sqlite_master
    var sql =
        "SELECT * FROM sqlite_master WHERE TYPE = 'table' AND NAME = 't_word'";
    var res = await database.rawQuery(sql);
    var returnRes = res != null && res.length > 0;
    return returnRes;
  }


  Future<void> checkTword() async {
    await database
        .execute("CREATE TABLE t_word (_id INTEGER PRIMARY KEY, _word TEXT, _meaning TEXT, _voice TEXT, _favorite INTEGER)");
  }

  Future<void> checkHword() async {
    await database
        .execute("CREATE TABLE h_word (_id INTEGER PRIMARY KEY, _sor INTEGER, _tar INTEGER, _text TEXT, _texted TEXT)");
  }

  Future<void> sqlInit2() async {
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, "demo.db");
    database = await openDatabase(path, version: 1);
  }

  Future<void> insertCase(
    String word,
      String menaning,
      String voice
  ) async {
    await database.transaction((txn) async {
      int id1 = await txn.rawInsert(
          'INSERT INTO t_word (_word, _meaning, _voice, _favorite) VALUES(?, ?, ?, ?)',
          [word, menaning, voice, 0]);
      print("inserted1: $id1");
    });
  }


  Future<void> insertHistory(
      String text,
      String texted,
      int sor,
      int tar
      ) async {
    await database.transaction((txn) async {
      int id1 = await txn.rawInsert(
          'INSERT INTO h_word (_sor, _tar, _text, _texted) VALUES(?, ?, ?, ?)',
          [sor, tar, text, texted]);
      print("inserted1: $id1");
    });
  }

  Future<List<Map>> getWordAll() async {
    List<Map> list = await database.rawQuery('SELECT * FROM t_word');
    return list;
  }

  Future<List<Map>> getWordFavorite() async {
    List<Map> list =
    await database.rawQuery('SELECT * FROM t_word WHERE _favorite = 1');
    return list;
  }

  Future<List<Map>> getWordByFavorite(int i) async {
    List<Map> list =
    await database.rawQuery('SELECT * FROM t_word WHERE _favorite = ?', [i]);
    return list;
  }

  Future<List<Map>> getWordByWord(String i) async {
    List<Map> list =
    await database.rawQuery('SELECT * FROM t_word WHERE _word = ?', [i]);
    return list;
  }

  Future<List<Map>> getAllHistory(String i) async {
    List<Map> list = await database.rawQuery('SELECT * FROM h_word');
    return list;
  }


  Future<void> updateById(int id, int favorite) async {
    int count = await database.rawUpdate(
        'UPDATE t_word SET _favorite = ? WHERE _id = ?', [favorite, id]);
  }

  Future<void> updateByWord(String w, int favorite) async {
    int count = await database.rawUpdate(
        'UPDATE t_word SET _favorite = ? WHERE _word = ?', [favorite, w]);
  }
}
